package com.agil.photoalbum.swing.admin;

import com.agil.photoalbum.db.dao.Functions;
import com.agil.photoalbum.db.entities.User;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField; 


public class CreateUserDialog extends JDialog{
	
	
	public CreateUserDialog(){
		this.setModal(true);
		this.setTitle("Add new User");
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setSize(new Dimension(600, 500));
		initComponents();
	}
	
	public void initComponents(){
                JLabel jlbusername = new JLabel("username");
                JLabel jlbpass = new JLabel("password");
                JLabel jlbpassconf = new JLabel("confirm");
		JLabel jlbfirst = new JLabel("First Name");
		JLabel jlblast = new JLabel("Last Name");
		JLabel jlbMail = new JLabel("E-mail address");		

                jtfusername = new JTextField();
                jtfpass = new JPasswordField();
                jtfpassconf = new JPasswordField();
		jtffirst = new JTextField(); 
		jtflast = new JTextField();
		jtfMail = new JTextField();
	
		JButton jbtAdd = new JButton("Add");
		jbtAdd.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        boolean areEqual = false;
                        char[] pass = jtfpass.getPassword();
                        char[] passconf = jtfpassconf.getPassword();
                        
                         for (int i = 0; i < pass.length; i++) {
                            if (pass[i] == passconf[i]) {
                                areEqual = true;
                            } else {
                                areEqual = false;
                            }                         
                        } 
                        String tUsername = jtfusername.getText();
                        String tMailAddress = jtfMail.getText();
                        boolean validMail = isValidEmail(tMailAddress);
                        boolean doesExists = Functions.userExists(tUsername);
                        if (validMail) { 
                            if (areEqual) {
                                if(!doesExists){
                                    User tUser = new User(tUsername, 
                                        jtfpass.getText(), jtffirst.getText(), jtflast.getText(), tMailAddress);
                                Functions.addUser(tUser); 
                                }else{
                                   JOptionPane.showMessageDialog(rootPane, "User already exists!", "Admin workspace", JOptionPane.WARNING_MESSAGE); 
                                }
                                
                            } else {
                                JOptionPane.showMessageDialog(rootPane, "Password fields are incorrect!", "Admin workspace", JOptionPane.WARNING_MESSAGE);
                            }
                        }else{ 
                            JOptionPane.showMessageDialog(rootPane, "Invalid email address!", "Admin workspace", JOptionPane.WARNING_MESSAGE);
                        }
                    }
	        });
		
		JPanel btnPanel = new JPanel(); 
		btnPanel.setLayout(new BorderLayout());
		
		btnPanel.add(jbtAdd, BorderLayout.CENTER);
		

		
		JPanel gridPanel = new JPanel();
		gridPanel.setLayout(new GridLayout(8,2,0,15));
                gridPanel.add(jlbusername);
                gridPanel.add(jtfusername);
                gridPanel.add(jlbpass);
                gridPanel.add(jtfpass);
                gridPanel.add(jlbpassconf);
                gridPanel.add(jtfpassconf);
		gridPanel.add(jlbfirst);
		gridPanel.add(jtffirst); 
		gridPanel.add(jlblast);
		gridPanel.add(jtflast); 
		gridPanel.add(jlbMail); 
		gridPanel.add(jtfMail);
		
		this.setLayout(new BorderLayout());
		this.add(gridPanel, BorderLayout.CENTER);
		this.add(btnPanel, BorderLayout.SOUTH);
		
	}

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CreateUserDialog().setVisible(true);
            }
        });
    }
    
    public boolean isValidEmail(String email) {
        String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*(.[_A-Za-z0-9-]+)";
        return email.matches(regex);
    } 
    
        private static JTextField jtfusername;
        private static JPasswordField jtfpass;
        private static JPasswordField jtfpassconf;
	private static JTextField jtffirst;
	private static JTextField jtflast;
	private static JTextField jtfMail;
}
